<script>
	import * as api from 'common/api.js';
	export default {
		globalData: {
			ForceUpdate: false
		},
		onLaunch: function() {
			console.log('App Launch');
			//this.checkLogin()
			/*setTimeout(() => {
				uni.setTabBarBadge({
					index: 1,
					text: '31'
				});
				uni.showTabBarRedDot({
					index: 3
				});
			}, 1000);*/
		},
		onShow: function() {
			console.log('App Show');
			//let themeType = uni.getStorageSync('themeType') ? uni.getStorageSync('themeType') : 'orange-theme';
			//this.cueGetTheme(themeType)
			//设置主题缓存
			//this.themeCache(themeType)
			//设置 全局背景色
			//this.getSystemBg()

		},
		onHide: function() {
			console.log('App Hide');
		},
		methods: {

			getToken() {
				var token
				try {
					token = uni.getStorageSync('token');
					if (token) {
						//console.log(token);
					} else {
						console.info("empty token,need login")
						setTimeout(function() {
							uni.navigateTo({
								url: "/pages/login/login"
							})
						}, 50)
					}
				} catch (e) {
					// error
				}
				return token
			},

			isAdmin() {
				var is_admin = 0
				var userinfo
				try {
					userinfo = uni.getStorageSync('userinfo');
					if (userinfo) {
						var obj = JSON.parse(userinfo)
						is_admin = obj.is_admin
					} else {
						console.info("empty userinfo,need login")
						setTimeout(function() {
							uni.navigateTo({
								url: "/pages/login/login"
							})
						}, 50)
					}
				} catch (e) {
					// error
				}
				return is_admin
			},

			getActions(func_url) {
				var that = this
				var promise = new Promise(function(resolve, reject) {
					var token = that.getToken()
					api.myRequest({
						url: '/Api/User/Actions',
						method: 'GET',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'Authorization': 'Bearer ' + token + ''
						},
						data: {
							func_url: func_url,
						},
					}, false).then(res => {
						resolve(res)
					}).catch(err => {
						console.log(err)
						reject(err)
					})
				});
				return promise
			},


			getUserInfo() {

				var userid, token
				try {
					userid = uni.getStorageSync('userid');
					token = uni.getStorageSync('token');
					if (userid && token) {
						console.log(userid, token);
					}
				} catch (e) {
					// error
				}
				api.myRequest({
					url: '/Api/User/Info',
					method: 'GET',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authorization': 'Bearer ' + token + ''
					},
					data: {},
				}, false).then(res => {
					//console.debug(res.code)
					if (res.code >= 0 && res.code != 401) {

						try {
							console.log("getUserInfo ok")
							uni.setStorageSync('userinfo', JSON.stringify(res.data));
							uni.$emit('userinfo_update', {
								msg: 'userinfo更新'
							})
						} catch (e) {
							console.error(e)
						}

					} else {
						console.info(res);
						// uni.showModal({
						// 	title: "失败",
						// 	content: res.msg,
						// 	showCancel: false,
						// 	confirmText: "确定"
						// })
						setTimeout(function() {
							uni.navigateTo({
								url: "/pages/login/login"
							})
						}, 50)

					}

				}).catch(err => {
					console.log(err)
				})
			},
			//获取服务器最新版本信息
			getRemoteVer() {
				var promise = new Promise(function(resolve, reject) {
					uni.request({
						header: {
							'content-type': 'application/x-www-form-urlencoded'
						},
						url: api.API_HEAD + "/Api/Public/AppInfo",
						method: 'GET',
						data: {},
						success: function(res) {
							if (res) {
								resolve(res.data);
							}
						},
						fail: (err) => {

							uni.showModal({
								title: '网络错误',
								content: '请重试或联系平台服务人员',
								showCancel: false
							})
							reject(err)
						},
						//请求结束之后，执行的回调函数（成功或失败都会执行）
						complete() {

						}
					});
				});
				return promise;
			},
		},

	};
</script>
<style lang="scss">
	@import '@/static/icon/iconfont.css';
	// @import '@/static/icon/iconfont-m.css';
	@import '@/uni_modules/uni-scss/index.scss';

	// 主题样式
	.css-theme {
		@include text-color();
		@include base-background();
		@include border-color();
		@include shadow-color();
	}

	.text-theme {
		@include text-color();
	}

	.bg-theme {
		@include base-background();
	}

	.border-theme {
		@include border-color();
	}

	.shadow-theme {
		@include shadow-color();
	}

	/* #ifndef APP-PLUS-NVUE */
	/* uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
	@import './common/uni.css';
	@import '@/static/customicons.css';
	@import "colorui/main.css";
	@import "colorui/icon.css";



	/* H5 兼容 pc 所需 */
	/* #ifdef H5 */
	@media screen and (min-width: 768px) {
		body {
			overflow-y: scroll;
		}
	}

	/* 顶栏通栏样式 */
	/* .uni-top-window {
	    left: 0;
	    right: 0;
	} */

	uni-page-body {
		background-color: #ffffff !important;
		min-height: 100% !important;
		height: auto !important;
	}

	.uni-top-window uni-tabbar .uni-tabbar {
		background-color: #ffffff !important;
	}

	.uni-app--showleftwindow .hideOnPc {
		display: none !important;
	}

	/* #endif */

	/* 以下样式用于 hello uni-app 演示所需 */
	page {
		background-color: #ffffff;
		// height: 100%;
		height: calc(100% - 50px);
		font-size: 28rpx;
		/* line-height: 1.8; */
	}

	// page{ height: 100% }

	/* #endif*/

	uni-tabbar,
	uni-tabbar .uni-tabbar {
		box-shadow: 1px -2px 6px rgba(0, 0, 0, 0.25);
	}
</style>
<style>
	/*每个页面公共css */
	.position {
		position: relative;
	}


	.text-main-color {
		color: #8799a3;
	}

	.refresh {
		padding: 6rpx 5rpx;
		border-radius: 6px;
	}
</style>